﻿using LightingServer.Tool;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Web;
using Excel = Microsoft.Office.Interop.Excel;

namespace LightingServer.TemplateGenerator
{
    public abstract class AbstractSpecGenerator : AbstractGenerator
    {
        public byte[] Generate(Lighting lighting, bool convertPDF = false)
        {
            Excel.Application application = null;

            Excel.Workbook wb = null;
            string exportFile = null;
            try
            {
                LogTool.Log("1 复制模板......");

                string dateTime = DateTime.Now.ToString("yyyyMMddHHmmss");
                exportFile = TemplateName + dateTime + ".xlsx";
                string sourceFile = Path.Combine(LightingServerContext.TemplateRoot, TemplateName + ".xlsx");
                string temFile = Path.Combine(LightingServerContext.ExportRoot, exportFile);
                File.Copy(sourceFile, temFile, true);

                application = new Excel.Application();
                application.Visible = false;

                LogTool.Log("2 加载模板......");
                wb = application.Workbooks.Open(temFile);
                Excel.Worksheet sheet = wb.Sheets[1];
                Excel.XmlMap map = SearchMap(wb, SpecMapName);

                if (lighting.HasPicture)
                {
                    LogTool.Log("3 插入图片......");
                    if (lighting.Picture.HasFacade && !string.IsNullOrEmpty(lighting.Picture.Facade))
                    {
                        string imageUrl = lighting.Picture.Facade.Remove(0, LightingServerContext.ImageUrl.Length);
                        imageUrl = Path.Combine(LightingServerContext.ImageRoot, imageUrl);
                        MappingRange(sheet, FacadeXpath, map, imageUrl);
                    }

                    if (lighting.Picture.HasSize && !string.IsNullOrEmpty(lighting.Picture.Size))
                    {
                        string imageUrl = lighting.Picture.Size.Remove(0, LightingServerContext.ImageUrl.Length);
                        imageUrl = Path.Combine(LightingServerContext.ImageRoot, imageUrl);
                        MappingRange(sheet, SizeXpath, map, imageUrl);
                    }

                    if (lighting.Picture.HasGrading1 && !string.IsNullOrEmpty(lighting.Picture.Grading1))
                    {
                        string imageUrl = lighting.Picture.Grading1.Remove(0, LightingServerContext.ImageUrl.Length);
                        imageUrl = Path.Combine(LightingServerContext.ImageRoot, imageUrl);
                        MappingRange(sheet, GradingXpath, map, imageUrl);
                    }

                    if (lighting.Picture.HasLive && !string.IsNullOrEmpty(lighting.Picture.Live))
                    {
                        string imageUrl = lighting.Picture.Live.Remove(0, LightingServerContext.ImageUrl.Length);
                        imageUrl = Path.Combine(LightingServerContext.ImageRoot, imageUrl);
                        MappingRange(sheet, LiveXpath, map, imageUrl);
                    }
                }

                LogTool.Log("4 插入数据......");
                string xmlData = ConvertToXml(lighting);
                map.ImportXml(xmlData);

                LogTool.Log("5 保存......");
                wb.Save();
                if (convertPDF)
                {
                    exportFile = TemplateName + dateTime + ".pdf";
                    //wb.SaveAs(pdfFile, Excel.XlFixedFormatType.xlTypePDF);
                    wb.ExportAsFixedFormat(
                    Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF,
                    Path.Combine(LightingServerContext.ExportRoot, exportFile),
                    Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityStandard,
                    false,
                    true,
                    1,
                    1,
                    false,
                    System.Reflection.Missing.Value);
                    try
                    {
                        File.Delete(temFile);
                    }
                    catch
                    {
                    }
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                try
                {
                    wb.Close();
                    Marshal.ReleaseComObject(wb);
                }
                catch
                {
                }

                try
                {
                    application.Quit();
                    Marshal.ReleaseComObject(application);
                    Kill(application);
                }
                catch
                {
                }
            }

            exportFile = Path.Combine(LightingServerContext.ExportUrl, exportFile);
            return Encoding.UTF8.GetBytes(exportFile);
        }

        protected abstract string ConvertToXml(Lighting lighting);

    }
}
